package org.jg.arkanoid.util;

public class Vector {
	private double x;
	private double y;
	
	public Vector(double x, double y){
		this.x = x;
		this.y = y;
	}
	
	public Vector(Vector startPoint, Vector endPoint){
		this.x = endPoint.x - startPoint.x;
		this.y = endPoint.y - startPoint.y;
	}
	
	public double getX() {
		return x;
	}
	
	public void setX(double x) {
		this.x = x;
	}
	
	public double getY() {
		return y;
	}
	
	public void setY(double y) {
		this.y = y;
	}
	
	public Vector add(int summand)
	{
		return new Vector(x+summand, y+summand );
	}
	
	public Vector addVector(Vector p){
		if(p == null)
			return this;
		
		return addVector(p.getX(), p.getY());
	}
	
	public Vector addVector(double x, double y) {
		return new Vector(this.x+x, this.y+y);
	}
	
	public Vector multiply(double m) {
		return new Vector(x*m, y*m);
	}
	
	public double legnth()
	{
		return Math.sqrt( x*x + y*y );
	}
	
	public double xAngle()
	{
		return Math.atan2(y, x);
	}
	
	public Vector turnVector( double angle )
	{
		double sinA = Math.sin(angle);
		double cosA = Math.cos(angle);
		
		return new Vector( cosA*x - sinA*y, sinA*x + cosA*y );
	}

	public Vector normalize()
    {
        double invLength = 1.0f / Math.sqrt(x*x + y*y);
        return multiply(invLength);
    }
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(x);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(y);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vector other = (Vector) obj;
		if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
			return false;
		if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
			return false;
		return true;
	}
	
}
